home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
usenet
/
sources
/
volume91
/
utilitys
/
sman_1_1
/
part01
/
src
/
sless_win.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-29
|
7KB
|
391 lines
#include <sless_defs.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <libraries/dosextens.h>
#include <intuition/intuition.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
Prototype BPTR makeoutput(char *);
Prototype void closeoutput(BPTR);
Prototype LONG stdpkt(LONG, LONG, BPTR);
Prototype void setupcon(BPTR);
Prototype void putconstr(char *, BPTR);
Prototype void putconchar(char, BPTR);
Prototype short getconchar(BPTR);
Prototype char rawreadcon(BPTR);
Prototype struct conreport * getconreport(short, BPTR);
Prototype struct conreport * readconreport(BPTR);
Prototype void windowresized(BPTR);
Prototype char * getstr(BPTR);
Prototype void findsize(int *, int *, BPTR);
Prototype void sto_char(short);
Prototype short get_sto(void);
Prototype void putconint(int, BPTR);
extern const char memory[];
static char *oldname;
extern int sx,sy;
extern char seek;
BPTR
makeoutput(n)
char *n;
{
BPTR con;
struct MsgPort *cport;
__aligned struct InfoData id;
struct Window *win;
char buff[77];
show_line;
if (!IsInteractive(con = Output())) {
strcpy(buff,"con:0/1/640/199/");
strncat(buff,n,60);
if (!(con = Open(buff,MODE_NEWFILE)))
leave(15,"sless: Unable to obtain a display.\n");
} else {
stdpkt(ACTION_DISK_INFO,(LONG)(&id >> 2),con);
win = (struct Window *)(id.id_VolumeNode);
oldname = win->Title;
SetWindowTitles(win,n,(char *)-1);
}
setupcon(con);
return con;
}
void
closeoutput(con)
BPTR con;
{
struct MsgPort *cport;
__aligned struct InfoData id;
struct Window *win;
show_line;
if (con != Output()) {
Close(con);
return;
}
stdpkt(ACTION_DISK_INFO,(LONG)(&id >> 2),con);
win = (struct Window *)(id.id_VolumeNode);
SetWindowTitles(win,oldname,(char *)-1);
stdpkt(ACTION_SCREEN_MODE,0,con);
putconstr("\x9b p",con);
}
LONG
stdpkt(type,arg,con)
LONG type,arg;
BPTR con;
{
__aligned struct StandardPacket pkt;
struct MsgPort *reply,*who;
show_line;
who = ((struct FileHandle *)(con << 2))->fh_Type;
if (!(reply = CreatePort(NULL,0)))
leave(15,"sless: Unable to send packet");
pkt.sp_Msg.mn_Node.ln_Name = (char *)&(pkt.sp_Pkt);
pkt.sp_Pkt.dp_Link = &(pkt.sp_Msg);
pkt.sp_Pkt.dp_Port = reply;
pkt.sp_Pkt.dp_Type = type;
pkt.sp_Pkt.dp_Arg1 = arg;
PutMsg(who,&(pkt.sp_Msg));
WaitPort(reply);
GetMsg(reply);
DeletePort(reply);
return pkt.sp_Pkt.dp_Res1;
}
void
setupcon(con)
BPTR con;
{
show_line;
stdpkt(ACTION_SCREEN_MODE,-1,con);
putconstr("\x9B20h\x9b12{",con);
}
struct conreport *
getconreport(e,con)
short e;
BPTR con;
{
char p;
struct conreport *cr;
show_line;
do {
while ((p = rawreadcon(con)) != (char)0x9B)
sto_char(p);
if (cr = readconreport(con)) {
if (cr->conid == e)
return cr;
else {
sto_char(cr->conid);
free(cr);
}
}
} while (TRUE);
}
struct conreport *
readconreport(con)
BPTR con;
{
struct conreport *cr;
char p;
int i=0,v;
show_line;
if (!(cr = malloc(sizeof(struct conreport))))
leave(12,memory);
do {
v = 0;
p = rawreadcon(con);
while (isdigit(p)) {
v = v * 10 + p - '0';
p = rawreadcon(con);
}
cr->args[i++] = v;
} while (p == ';');
v = makeconid(p,0);
if (p == '?' || p == ' ') {
p = rawreadcon(con);
v = v + p;
}
cr->conid = v;
if (v == makeconid('|',0) && cr->args[0] == 12) {
windowresized(con);
return NULL;
}
return cr;
}
short
getconchar(con)
BPTR con;
{
short p;
struct conreport *cr;
show_line;
do {
if (p = get_sto())
break;
if ((p = rawreadcon(con)) == (char)'\x9b') {
if (cr = readconreport(con)) {
p = cr->conid;
free(cr);
break;
}
} else
break;
} while (TRUE);
return p;
}
char
rawreadcon(con)
BPTR con;
{
char p;
show_line;
while (!WaitForChar(con,100000)) {
chkabort();
}
if (Read(con,&p,1) != 1)
return 0;
return p;
}
void
putconchar(c,con)
char c;
BPTR con;
{
show_line;
Write(con,&c,1);
chkabort();
}
void
putconstr(p,con)
char *p;
BPTR con;
{
show_line;
Write(con,p,strlen(p));
chkabort();
}
void windowresized(con)
BPTR con;
{
show_line;
findsize(&sx,&sy,con);
if (seek)
refreshscreen();
else {
bottomline();
putconstr(": ",con);
}
}
char *
getstr(con)
BPTR con;
{
short c;
char buff[200],*p = buff,*q;
show_line;
*p = 0;
do {
switch(c = getconchar(con)) {
case (13) :
goto end;
case (makeconid('D',0)) :
if (p != buff) {
putconstr("\x9bD",con);
p--;
}
break;
case (makeconid('C',0)) :
if (*p) {
putconstr("\x9bC",con);
p++;
}
break;
case (makeconid(' ','A')) :
if (p != buff) {
putconchar('\x9b',con);
putconint(p-buff,con);
putconchar('D',con);
p = buff;
}
break;
case (makeconid(' ','@')) :
if (*p) {
putconchar('\x9b',con);
putconint(strlen(p),con);
putconchar('C',con);
p += strlen(p);
}
break;
case (8) :
if (p != buff) {
putconstr("\x9bD\x9bP",con);
q = --p;
while (*q = *(q+1))
q++;
}
break;
case (127) :
if (*p) {
putconstr("\x9bP",con);
q = p;
while (*q = *(q+1))
q++;
}
default :
if (isprint(c)) {
putconstr("\x9b@",con);
putconchar(c,con);
q = p + strlen(p) + 1;
do {
*q = *(q-1);
q--;
} while (q != p);
*p++ = c;
}
}
} while (TRUE);
end:
if (strlen(buff) == 0)
return NULL;
if (!(p = malloc(strlen(buff)+1)))
leave(12,memory);
strcpy(p,buff);
return p;
}
void
putconint(x,con)
int x;
BPTR con;
{
int v;
char p,f = FALSE;
show_line;
for (v = 1000000; v > 0; v /= 10) {
p = x / v;
x = x - p*v;
if (p != 0 || f) {
f = TRUE;
putconchar(p + '0',con);
}
}
}
void
findsize(x,y,con)
int *x,*y;
BPTR con;
{
struct conreport *cr;
show_line;
putconstr("\x9b0 q",con);
cr = getconreport(makeconid(' ','r'),con);
*x = cr->args[3];
*y = cr->args[2];
free(cr);
}
#define STO_LEN 100
static short sto_buff[STO_LEN];
static int sto_s = 0,sto_e = 0;
void
sto_char(e)
short e;
{
show_line;
sto_buff[sto_e] = e;
sto_e = (sto_e + 1) % STO_LEN;
if (sto_e == sto_s) /* lost some off the end */
sto_s = (sto_s + 1) % STO_LEN;
}
short
get_sto()
{
short c;
show_line;
if (sto_s == sto_e)
return 0;
c = sto_buff[sto_s];
sto_s = (sto_s + 1) % STO_LEN;
return c;
}